function news_image () {
    this.remove = function (image_url) {
        
        if ($("img.img_existing").length <= 1) alert("At least one image is required");
        else {
        
            var confirm_delete = confirm("Detete " + image_url + "?");
            if (confirm_delete) {
                $.ajax({
                    async: false,
                    type: 'POST',
                    url: "/modules/news/delete_image.php",
                    data: {image_url : image_url},
                    success: function (result) {
                        if (result == "success") {
                            alert(image_url + " deleted");
                            $("#news_left_box").click();
                            
                        }
                        else alert("error deleting " + image_url +"\n" + result);
                        return result;
                    }
                });
            }
        }
   }
}

function news () {
    this.edit = {
        banner_dir : "/modules/news/banners/",
        index : 0,
        pic_index : Array(),
        current_image : "",
        text_updated : false,
        initialize : function () {
            /*$("#edit_content_area").css("visibility", "visible");
            $("#edit_menu_bar").css("visibility", "hidden");
            $("#menu_data").css("visibility", "hidden");
            $("#edit_content_area").children().each (function () {
                $(this).css("visibility", "visible");
            })*/
            
            /*show_toolbar_edit();
            admin_edit_article();*/
        },
        remove_timestamp : function (full_url) {
            var url = full_url.split("?");
            return url[0];
        },
        load : function (id) {
            this.text_updated = false;
            $("#news_left_box").html("<img src=\"" + this.banner_dir + this.pic_index[id]  + "?timestamp=" + new Date().getTime()  + "\">");
            this.current_image = this.pic_index[id];
            $.ajax({
                async: false,
                type: 'POST',
                url: "/modules/news/get_text.php",
                data: {id : id},
                success: function (result) {
                    $("#news_right_box").html(result);
                   
                }
            });
        },
        next : function () {
            this.save ();
            
            this.index++;
            if (this.index == this.pic_index.length) this.index = 0;
            this.load (this.index);
            
            
        },
        save : function () {
            var data_update = "";
            if (this.remove_timestamp($("#news_left_box img").attr("src")) != this.banner_dir + this.current_image) {
                $.ajax({
                    async: false,
                    type: 'POST',
                    url: "/modules/news/update_image.php",
                    data: {
                        old_image : this.banner_dir + this.current_image,
                        new_image : $("#news_left_box img").attr("src")
                    },
                    success: function (result) {
                        if (jQuery.trim(result) == "success") {
                            $("#news_left_box img").removeAttr("src").attr(this.banner_dir + this.current_image + "?timestamp=" + new Date().getTime());                          
                            data_update = "";
                        } else data_update = result;
                    }
                });
            }
            if (this.text_updated == true) {
               $.ajax({
                    async: false,
                    type: 'POST',
                    url: "/modules/news/update_text.php",
                    data: {
                        html :      $("#news_right_box").html(),
                        image_id :  $("#news_left_box img").attr("src")
                    },
                    success: function (result) {
                       alert (result);
                    }
                });
            }
            
            if (data_update != "") alert(data_update);
        },
        image_selection : {
             image_list : {},
             initialize : function () {
                 if ($("#image_update_area").length > 0) {
                      $.ajax({
                        async: false,
                        type: 'POST',
                        url: "/modules/news/get_image_list.php",
                        datatype: "json",
                        success: function (result) {
                            
                            var html = "<div><img id=\"btn_close_news_image\" src=\"/modules/toolbar/templates/create/images/icons/cross.png\"></div>"
                                        + "<div>News Images</div>";
                            
                            
                            this.image_list = jQuery.parseJSON(result);                           
                            
                            var i =0;
                            for(_obj in this.image_list) {
                                i++;
                                
                                if ((typeof this.image_list[_obj] != "function")&&(this.image_list[_obj] != "undefined")) {
                                    var image_url = "/modules/news/banners/" + this.image_list[_obj];
                                    html += "<div class=sh_image_label>" + this.image_list[_obj] +  "</div>"
                                        + "<div>"
                                        + "<img class=img_existing src=\"" + image_url + "\">"
                                        + "<iframe width=\"350px\" frameborder=0 src=\"/modules/news/upload_image.php?id=" + i  +"\">"
                                        + "</iframe>"
                                        + "<img image_url=\"" + image_url + "\" class=news_img_delete src=\"/modules/toolbar/templates/create/images/icons/delete.png\">"
                                        + "</div>";
                                
                                        //html = "<iframe></iframe"
                                    //$("#section_head_image_area").append(this.image_list[_obj]);
                                }
                            }
                            
                            for (j=i;j<=4;j++) {
                                html    += "<div class=sh_image_label>New</div>"
                                        + "<div>"
                                        + "<iframe width=\"350px\"  frameborder=0 src=\"/modules/news/upload_image.php?id=" + j  +"\"></iframe>"
                                        + "</div>";
                            }
                           
                            $("#image_update_area").css ({
                                display: "block",
                                "background-color": "silver",
                                "z-index" : 500,
                                "border-width":"2px",
                                "border-style":"outset",
                                position: "absolute"
                            })                            
                
                            $("#image_update_area").html(html);
                
                            $("#btn_close_news_image").click (function () { $("#image_update_area").css("display","none")});
                            $("#btn_close_news_image").css ({cursor: "pointer"})                            
                            
                            $(".sh_image_label").css ({
                                "font-size" : "75%"
                            });
                
                            $("#image_update_area .img_existing").css ({
                                width : "15%",
                                "float" : "left",
                                "margin-right":"2px"
                            });
                
                            $("#image_update_area .news_img_delete").css ({
                                "float" : "right",
                                "margin-top" : "10px",
                                cursor: "pointer"
                            });
                            
                            _news_image = new news_image();
                             $(".news_img_delete").click (function () {_news_image.remove($(this).attr("image_url"))});
                             
                             
                        }                      
                    })    
                }    
            }
        }
    }
    $(document).ready(function () {
     /*   $("[id*=news_text_] ").each(function(){
           if ($(this).attr("id") != "news_text_box") {
           var html = $(this).html().split(" ");
           alert($(this).attr("id"))
           }
        })*/
    })
}

